<template>
  <div class="login-wrap">
    <div class="ms-title">用户登录</div>
    <div class="ms-login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="password"
            v-model="ruleForm.password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >登录</el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>
<script>
import * as api from "@/API/require"
export default {
  data: function() {
    return {
      checked: true,
      ruleForm: {
        username: '',
        password: '',
        verifycode: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        verifycode: [
          { required: false, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  created(){
      if(localStorage.getItem('ms_username')){
          this.ruleForm.username = localStorage.getItem('ms_username')
      }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
            api.login({
                username:this.ruleForm.username,
                password:this.ruleForm.password
            }).then(res=>{
                this.$message(res.data.message)                  
                if(res.data.result == 1){
                    this.$router.push('/');
                    localStorage.setItem("token",res.data.token)
                    localStorage.setItem("id",res.data.id)
                    localStorage.setItem('ms_username', this.ruleForm.username);
                }
            })      
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /*background:cornflowerblue;*/
  background: steelblue;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 250px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
  border: 1px solid #ccc;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
.remember {
  margin: 3px 0 10px 0;
}
</style>
